package com.zhufei.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhufei.dao.CompanyDao;

public class companyServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public companyServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String action = request.getParameter("action");
		if(action == null){
			
		}
		if(action.equals("signIn")){
			this.signIn(request,response);
		}
	}

	private void signIn(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf8");
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String linkMan = request.getParameter("linkman");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String remark = request.getParameter("remark");
		
		CompanyDao companyDao = new CompanyDao();
		boolean isSucc = companyDao.insertCompany(name, linkMan, phone, email, remark);
		if(isSucc){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '感谢您的登记，我们会在线下与您联系！ ');window.close(); </script> "); 
			out.flush(); 
			out.close();	
		}else{
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '登记失败！ ');window.location.href= 'corporationSignIn.jsp'; </script> "); 
			out.flush(); 
			out.close();	
		}
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
